package com.ruoyi.web.controller.TiebaForums.controller;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.mapper.SysLogininforMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.web.controller.TiebaChat.domain.TiebaChat;
import com.ruoyi.web.controller.TiebaForums.domain.TiebaForums;
import com.ruoyi.web.controller.TiebaForums.service.ITiebaForumsService;
import com.ruoyi.web.controller.TiebaForums.vo.TiebaForumsVo;
import com.ruoyi.web.controller.TiebaForums.vo.UserTiebaForumsAdd;
import com.ruoyi.web.controller.TiebaPosts.domain.TiebaPosts;
import com.ruoyi.web.controller.TiebaPosts.service.ITiebaPostsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = "用户-贴吧")
@RestController
@RequestMapping("/TiebaForums/UserTiebaForums")
public class UserTiebaForumsController extends BaseController {

    @Autowired
    private ITiebaForumsService tiebaForumsService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Autowired
    private ITiebaPostsService tiebaPostsService;


    /**
     * 查询贴吧管理列表
     */
    @ApiOperation("查询所有的贴吧")
//    @PreAuthorize("@ss.hasPermi('TiebaForums:TiebaForums:list')")
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list() {
        startPage();
        List<TiebaForums> list = tiebaForumsService.selectTiebaForumsList2();
        List<TiebaForumsVo> tiebaForumsVos = new ArrayList<>();
        for (TiebaForums tiebaForums : list) {
            SysUser sysUser = sysUserMapper.selectUserById(tiebaForums.getUserId());
            // 所有的帖子
            TiebaPosts tiebaPosts = new TiebaPosts();
            tiebaPosts.setForumId(tiebaForums.getId());
            List<TiebaPosts> posts = tiebaPostsService.selectTiebaPostsList3(tiebaPosts);
            // 总评论数量
            long allCommentCount = 0L;
            // 总的点赞数量
            long allLoveCount = 0L;
            // 总的分享数量
            long allShareCount = 0L;
            // 最后一条帖子
            TiebaPosts lastPost = null;
            // 累加帖子的评论、点赞和分享数量
            for (TiebaPosts post : posts) {
                allCommentCount += post.getCommentCount() != null ? post.getCommentCount() : 0;
                allLoveCount += post.getLoveCount() != null ? post.getLoveCount() : 0;
                allShareCount += post.getShareCount() != null ? post.getShareCount() : 0;
                // 假设帖子列表是按时间排序的，最后一条帖子在列表末尾
                if (lastPost == null || post.getCreateTime().after(lastPost.getCreateTime())) {
                    lastPost = post;
                    System.out.println("123456789");
                    System.out.println(lastPost);
                }
            }
            // 重构数据
            TiebaForumsVo tiebaForumsVo = new TiebaForumsVo();
            tiebaForumsVo.setId(tiebaForums.getId());
            tiebaForumsVo.setForumsCount(tiebaForums.getForumsCount());
            tiebaForumsVo.setGuanzhuCount(tiebaForums.getGuanzhuCount());
            tiebaForumsVo.setImageUrl(tiebaForums.getImageUrl());
            tiebaForumsVo.setUserImageUrl(sysUser.getAvatar());
            tiebaForumsVo.setBazhuName(tiebaForums.getBazhuName());
            tiebaForumsVo.setDescription(tiebaForums.getDescription());
            tiebaForumsVo.setName(tiebaForums.getName());
            tiebaForumsVo.setAllShareCount(allShareCount);
            tiebaForumsVo.setAllCommentCount(allCommentCount);
            tiebaForumsVo.setAllLoveCount(allLoveCount);
            tiebaForumsVos.add(tiebaForumsVo);
            // 添加最后一条帖子的数据（如果存在）
            if (lastPost != null) {
                tiebaForumsVo.setLastPostId(lastPost.getId());
                tiebaForumsVo.setLastPostTitle(lastPost.getTitle());
                tiebaForumsVo.setLastPostContent(lastPost.getContent());
                tiebaForumsVo.setLastPostCreateTime(lastPost.getCreateTime());
                tiebaForumsVo.setLastPostImage(lastPost.getImageUrl());
                tiebaForumsVo.setLastPostCollectCount(lastPost.getCollectCount());
                tiebaForumsVo.setLastPostGuanzhuCount(lastPost.getGuanzhuCount());
            }

        }
        return getDataTable(tiebaForumsVos);
    }

    /**
     * 获取贴吧管理详细信息
     */
    @ApiOperation("用户查询详贴吧信息")
    @PreAuthorize("@ss.hasPermi('TiebaForums:TiebaForums:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tiebaForumsService.getById(id));
    }

    /**
     * 新增贴吧管理
     */
    @ApiOperation("用户创建贴吧")
    @PreAuthorize("@ss.hasPermi('TiebaForums:TiebaForums:add')")
    @Log(title = "贴吧管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserTiebaForumsAdd userTiebaForumsAdd) {
        TiebaForums tiebaForums = new TiebaForums();
        tiebaForums.setUserId(getUserId());
        tiebaForums.setDeptId(getDeptId());
        tiebaForums.setDescription(userTiebaForumsAdd.getDescription());
        tiebaForums.setName(userTiebaForumsAdd.getName());
        tiebaForums.setBazhuName(getUsername());
        tiebaForums.setImageUrl(userTiebaForumsAdd.getImageUrl());
        tiebaForums.setForumsCount(0L);
        tiebaForums.setGuanzhuCount(0L);
        return toAjax(tiebaForumsService.save(tiebaForums));
    }

    /**
     * 修改贴吧管理
     */
    @ApiOperation("用户修改贴吧")
    @PreAuthorize("@ss.hasPermi('TiebaForums:TiebaForums:edit')")
    @Log(title = "贴吧管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserTiebaForumsAdd userTiebaForumsAdd) {
        TiebaForums tiebaForums = new TiebaForums();
        tiebaForums.setUserId(getUserId());
        tiebaForums.setDeptId(getDeptId());
        tiebaForums.setDescription(userTiebaForumsAdd.getDescription());
        tiebaForums.setName(userTiebaForumsAdd.getName());
        tiebaForums.setBazhuName(getUsername());
        tiebaForums.setImageUrl(userTiebaForumsAdd.getImageUrl());
        return toAjax(tiebaForumsService.updateById(tiebaForums));
    }

    /**
     * 删除贴吧管理
     */
    @ApiOperation("用户删除贴吧")
    @PreAuthorize("@ss.hasPermi('TiebaForums:TiebaForums:remove')")
    @Log(title = "贴吧管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tiebaForumsService.removeByIds(Arrays.asList(ids)));
    }

}
